﻿Ext.define('app.prm.project.EditProduct',
{
    extend: 'Ext.window.Window',
    title: '选择产品',
    autoScroll: true,
    modal: true,
    maximized: true,
    layout: 'border',
    initComponent: function () {
        var me = this;
        me.items = [
            {
                xtype: 'panel',
                region: 'west',
                width: 650,
                split: true,
                border: false,
                layout: 'border',
                items: [
                    me.getSearchPanel(),
                    {
                        xtype: 'panel',
                        border: false,
                        region: 'center',
                        layout: 'fit',
                        // bodyStyle: 'padding:10px',
                        items: [
                            me.getGrid()
                        ]
                    }
                ]
            },
            {
                region: 'center',
                border: false,
                layout: 'border',
                items: [
                    me.getChemPanel(),
                    me.getChemForm()
                ]
            }
        ];
        me.buttons = [
            { text: '保存', scope: this, handler: me.onSubmit },
            { text: '关闭', handler: function () { me.close(); } }
        ]
        me.callParent();
    },
    getSearchPanel: function () {
        var me = this;
        if (!this.searchPanel) {
            this.searchPanel = new Ext.panel.Panel({
                border: false,
                region: 'north',
                height: 100,
                bodyStyle: 'padding:30px',
                layout: 'hbox',
                items: [
                    {
                        flex: 6.7,
                        xtype: 'textfield',
                        id: 'txt_Search',
                        anchor: '100%',
                        emptyText: '中文名、英文名、CAS号检索'
                    },
                    {
                        flex: 1,
                        xtype: 'button',
                        style: 'margin:0 5px 0 5px',
                        text: '查询',
                        handler: function ()
                        {
                            var keyword = Ext.getCmp("txt_Search").getValue();
                            me.getGrid().store.filter = { cas: keyword, cnName: keyword, enName: keyword };
                            me.getGrid().store.load();
                        }
                    },
                    {
                        flex: 2,
                        xtype: 'button',
                        text: '高级查询'
                    }
                ]
            });
        }
        return this.searchPanel;
    },
    getGrid: function () {
        if (!this.gridPanel) {
            var store = Ext.create("app.ux.PagePrmStore", { url: '/api/services/app/substance/GetAll' });
            this.gridPanel = new Ext.grid.Panel({
                forceFit: true,
                store: store,
                border: true,
                bbar: new Ext.toolbar.Paging({
                    store: store,
                    displayInfo: true,
                    displayMsg: "显示{0}-{1}条,共计{2}条",
                    emptyMsg: "没有数据"
                }),
                columns: [
                    { header: "结构式", dataIndex: 'projectNo' },
                    { header: "中文名", dataIndex: 'cnName' },
                    { header: "英文名", dataIndex: 'enName' },
                    { header: "CAS", dataIndex: 'cas' },
                    { header: "分子式", dataIndex: 'formula' },
                    { header: "分子量", dataIndex: 'molecularWeight' },
                    { header: "高风险", dataIndex: 'highRisk' }
                ],
                listeners: {
                    itemclick: function (grid, record, item, index, e, eOpts) {
                        App.AjaxGet('/api/services/app/substance/GetById?id=' + record.data.id, function (data) {
                            var json = data.result;
                            Ext.getCmp("txt_Cas").setValue(json.cas);
                            Ext.getCmp("txt_Formula").setValue(json.formula);
                            Ext.getCmp("txt_MolecularWeight").setValue(json.molecularWeight);
                            Ext.getCmp("txt_CnName").setValue(json.cnName);
                            Ext.getCmp("txt_EnName").setValue(json.enName);
                            Ext.getCmp("txt_HighRisk").setValue(json.highRisk);
                            Ext.getCmp("txt_Note").setValue(json.note);
                            Ext.getCmp("txt_Id").setValue(json.id);
                            Ext.getCmp("txt_Product").store.loadData(json.products);
                        });
                    }
                }
            });
        }
        return this.gridPanel;
    },
    getChemPanel: function () {
        if (!this.chemPanel) {
            this.chemPanel = new Ext.panel.Panel({
                region: 'north',
                height: 200,
                tbar: [
                    "->",
                    { text: '编辑', iconCls: 'edit', handler: function () { } },
                    { text: '粘贴', iconCls: 'paste', handler: function () { } },
                    { text: '删除', iconCls: 'delete', handler: function () { } }
                ],
                bodyStyle: 'padding:30px',
                items: []
            });
        }
        return this.chemPanel;
    },
    getChemForm: function () {
        var me = this;
        if (!this.chemForm) {
            this.chemForm = new Ext.form.Panel({
                defaultType: 'textfield',
                border: false,
                title: '物质信息',
                region: 'center',
                fieldDefaults: {
                    labelWidth: 90,
                    labelAlign: "right",
                    flex: 1,
                    margin: 6
                },
                items: [
                    {
                        xtype: 'container',
                        anchor: '100%',
                        layout: 'hbox',
                        items: [
                            {
                                xtype: 'container',
                                flex: 1,
                                layout: 'anchor',
                                items: [
                                    {
                                        xtype: 'hidden',
                                        id: 'txt_Id',
                                        name: 'SubstanceId',
                                        value: 0
                                    },
                                    {
                                        xtype: 'textfield',
                                        fieldLabel: 'CAS',
                                        id: 'txt_Cas',
                                        name: 'CAS'
                                    },
                                    {
                                        xtype: 'textfield',
                                        id: 'txt_Formula',
                                        fieldLabel: '分子式',
                                        name: 'Formula'
                                    }
                                ]
                            },
                            {
                                xtype: 'container',
                                flex: 1,
                                layout: 'anchor',
                                items: [
                                    {
                                        xtype: 'combo',
                                        valueField: 'id',
                                        displayField: 'code',
                                        queryMode: 'local',
                                        triggerAction: 'all',
                                        store: new Ext.data.Store({
                                            data: [],
                                            fields: [{ name: 'id' }, { name: 'code' }]
                                        }),
                                        id: 'txt_Product',
                                        fieldLabel: '产品号',
                                        name: 'ProductNo',
                                        listeners: {
                                            select: function (cbo, record, e) {
                                                Ext.getCmp("txt_ProductId").setValue(record.data.id);
                                            }
                                        }
                                    },
                                    {
                                        xtype: 'hidden',
                                        id: 'txt_ProductId',
                                        name: 'ProductId',
                                        value: 0
                                    },
                                    {
                                        xtype: 'textfield',
                                        id: 'txt_MolecularWeight',
                                        fieldLabel: '分子量',
                                        name: 'MolecularWeight'
                                    }
                                ]
                            }
                        ]
                    },
                    {
                        xtype: 'textarea',
                        fieldLabel: '中文名',
                        height: 50,
                        id: 'txt_CnName',
                        anchor: '75%',
                        name: 'CnName'
                    },
                    {
                        xtype: 'textarea',
                        fieldLabel: '英文名',
                        height: 50,
                        anchor: '75%',
                        id: 'txt_EnName',
                        name: 'EnName'
                    },
                    {
                        xtype: 'textarea',
                        fieldLabel: '高风险',
                        height: 50,
                        id: 'txt_HighRisk',
                        anchor: '75%',
                        name: 'HighRisk'
                    },
                    {
                        xtype: 'textarea',
                        fieldLabel: '备注',
                        height: 50,
                        id: 'txt_Note',
                        anchor: '75%',
                        name: 'Note'
                    }
                ]
            });
        }
        return this.chemForm;
    },
    onSubmit: function () {
        var me = this;
        var form = me.getChemForm().getForm().getValues();
        App.AjaxPost('/api/services/app/substance/SetSubstanceProduct', form, function (data) {
            var productNo = Ext.getCmp("txt_Product").getRawValue();
            me.fireEvent("savesuccess", productNo);
        });
    }
});